package com.hellpro.lolbox.video.view.fragment.view;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.LolBaseActivity;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.video.bean.ClassifySecondInfoBean;
import com.hellpro.lolbox.video.model.ClassifySecondLvAdapter;
import com.hellpro.lolbox.video.presenter.ClassifyPresenter;
import com.hellpro.lolbox.video.view.fragment.IClassifySecondActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class VideoClassifySecondActivity extends LolBaseActivity implements IClassifySecondActivity {


    @BindView(R.id.main_video_iv_return)
    ImageView mainVideoIvReturn;
    @BindView(R.id.video_tool_bar_name)
    TextView videoToolBarName;
    @BindView(R.id.video_classify_lv)
    PullToRefreshListView videoClassifyLv;
    private String url;
    private ClassifyPresenter presenter;
    private List<ClassifySecondInfoBean> data;
    private ClassifySecondLvAdapter adapter;
    private int p = 1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_video_classify_second;
    }

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 10) {
                data = (List<ClassifySecondInfoBean>) msg.obj;
                adapter.setData(data);
            }
        }
    };


    @Override
    public void initView() {
        Intent intent = getIntent();
        String tag = intent.getStringExtra("url");
        String name = intent.getStringExtra("name");
        videoToolBarName.setText(name);
        url = String.format(UrlConfigs.VIDEO_BASEURL, tag)+"";
        Log.i("TAG", "+++++++++++++++++" + url);
        presenter = new ClassifyPresenter(this);
        presenter.loadClassifyInfo(p);
        adapter = new ClassifySecondLvAdapter();
        videoClassifyLv.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        videoClassifyLv.setAdapter(adapter);
        videoClassifyLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                p += 1;
                if (p <= 5) {
                    presenter.loadClassifyInfo(p);
                    videoClassifyLv.onRefreshComplete();
                } else {
                    Toast.makeText(VideoClassifySecondActivity.this, "当前没有更多数据", Toast.LENGTH_SHORT).show();
                    videoClassifyLv.onRefreshComplete();
                }
            }
        });
    }

    @Override
    public String getVideoUrl() {
        return url;
    }

    @Override
    public PullToRefreshListView getVideoLv() {
        return videoClassifyLv;
    }

    @Override
    public Context getVideoContext() {
        return VideoClassifySecondActivity.this;
    }

    @Override
    public Handler getClassifyHandler() {
        return mHandler;
    }

    @Override
    public List<ClassifySecondInfoBean> getData() {
        return data;
    }

    @OnClick(R.id.main_video_iv_return)
    public void onClick() {
        finish();
    }
}
